<template>
  <span>
    <el-dialog title="管理员权限修改" :visible.sync="dialogFormVisible" @close='handleClose'>
      <!-- 表单 -->
      <el-form :model="form" status-icon :rules="rules" ref="form" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名：" prop="userName">
          <el-input placeholder="请输入用户名" v-model="form.userName" disabled='disabled'></el-input>
        </el-form-item> 
        <el-form-item label="角色名称">
          <el-select v-model="form.selected" placeholder="请选择角色">
            <el-option 
              v-for="item in form.roleName"
              :key="item.id"
              :label="item.rolename" 
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>              
      </el-form>
      <!-- 确认按钮 -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </div>
    </el-dialog>
  </span>  
</template>
<script>
export default {
  name:'adminModifyRole',  
  data() {
    return {
      form: {
        userName:'',
        roleName:[],
        selected:'',
        id:''
      },
      dialogFormVisible: true,
      formLabelWidth: '80px',
      rules: {
        userName: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ]
      }    
    }
  },
  created(){
    const id=this.$route.query.id
    this.form.userName=id[0].username
    this.form.selected=id[0].roleid
    this.form.id=id[0].id
    this.getRolemsg()
  },
  methods:{ 
    getRolemsg(){
      this.axios.get('/api/role/getalldata/').then((res)=>{
        if(res.data.code==0){
          this.form.roleName=res.data.data
        }else{
          this.message('服务器忙，请稍后重试','error')
        }
      }).catch((error)=>{
        this.message('服务器忙，请稍后重试','error')
      })
    },
    postAdimnData(){
      this.axios.post('/api/admin/edit',{
        params:{
          id:this.form.id,
          username:this.form.userName,
          roleid:this.form.selected
        }
      }).then((res)=>{
        // 修改成功
        this.message('修改成功','success')
      }).catch((error)=>{
        this.message('服务器忙，请稍后重试','error')
      })
    },   
    handleClose(){
      this.$router.go(-1)
    },
    handleCancel(){
      this.dialogFormVisible=false
    },
    submitForm(ruleForm){
      this.dialogFormVisible = false
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          // 提交修改
          this.postAdimnData()          
        } else {
          this.message('服务器忙，请稍后重试','error')
          return false;
        }
      });
    },
     // 失败提示
    message(msg,type){
      this.$message({
        message:msg,
        type
      })
    }
  }
}
</script>
<style scoped>

</style>